package SocketBaseTest;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by yexuan on 2018/3/24.
 * 实现tcp文件复制
 */
public class TextClient {
    public static void main(String[] args) throws Exception{
        Socket s = new Socket("172.16.35.23",10003);
        BufferedReader br = new BufferedReader(new FileReader("demo.txt"));

        PrintWriter pw = new PrintWriter(s.getOutputStream(),true);  //true为自动刷新
        String line = null;
        while ((line=br.readLine())!=null){
            pw.println(line);
        }
        s.shutdownOutput();//禁用此套接字的输出流。也就是给数据一个而结束符
        BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = brIn.readLine();
        System.out.println(str);

        br.close();
        s.close();
    }
}

class TextServer{
    public static void main(String[] args) throws Exception{
        ServerSocket ss = new ServerSocket(10003);
        Socket s = ss.accept();
        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip + "connect.....");

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter pw = new PrintWriter(new FileWriter("tcpText.txt"),true);

        String line = null;
        while ((line=br.readLine())!=null){
            pw.println(line);
        }

        PrintWriter pw2 = new PrintWriter(s.getOutputStream(),true);
        pw2.println("上传成功");

        pw.close();
        s.close();
        ss.close();
    }
}